package com.gthncz;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class GenericTypeDemo {

    private static class Genera<T> {

        public T data;

        public Genera(T data){
            this.data = data;
        }

        public void setData(T data){
            this.data = data;
        }

        public T getData() {
            return data;
        }
    }

    public static void set(List<Object> objects){

    }

    public static void main(String[] args){
        List<Object> objects = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        // always true. 内部是泛型. 编译后都是 Object 类型.
        System.out.println("objects == strings? " + (objects.getClass() == strings.getClass()));
        for(Method method: strings.getClass().getMethods()){
            System.out.println(method.toString());
        }
        // list.add() 方法的签名是 java.lang.Object
        strings.add("hello");
        try {
            Method add = strings.getClass().getDeclaredMethod("add", Object.class);
            add.invoke(strings, 123);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        for(Object e: strings){
            // 输出: "hello", 123
            // 表示可以利用 relect 绕过 类型安全检查!
            System.out.println(e);
        }

        set(objects);
        // set(strings);  报错

        ArrayList<Object> arrayList = new ArrayList<>();
        LinkedList<Object> linkedList = new LinkedList<>();

        Genera<String> stringGenera = new Genera<String>("gt");
        stringGenera.setData("hello");

        System.out.println("name: " + stringGenera.getClass().getCanonicalName());

        for (Method method: stringGenera.getClass().getMethods()){
            System.out.println(method.toString());
        }

    }

}
